<template>
	<view>
		<button v-if="!avator" @click="login">一件登录</button>

		<view class="" v-else>
			<image :src="avator" mode=""></image>
			<text>{{ nickname }}</text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				nickname: '',
				avator: ''
			};
		},
		methods: {
			async login() {
				// 获取code
				const [error, resp] = await uni.login()
				// 独立后端发送数据
				const code = resp.code;
				const data = {
					js_code: code,
					appid: 'wx2bae7e5b372f752d',
					secret: '00778fc71ba13c83521c512e78045cfe',
					grant_type: 'authorization_code'
				}
				const [error1, resp1] = await uni.request({
					url: 'https://api.weixin.qq.com/sns/jscode2session',
					method: 'GET',
					data: data
				})
				// localStorage.setItem('openid', )
				uni.setStorageSync('openid', resp1.data.openid)
				const [error2, resp2] = await uni.getUserInfo({

				})
				console.log(resp2)
				this.nickname = resp2.userInfo.nickname
				this.avator = resp2.userInfo.avatarUrl
			}
		}
	}
</script>

<style lang="scss">

</style>